package com.airlines.system.service.impl;

import com.airlines.system.dao.SysMenuMapper;
import com.airlines.system.domain.SysMenu;
import com.airlines.system.service.SysMenuService;

import com.airlines.system.vo.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findByRoleId(String roleId) {
        return sysMenuMapper.findByRoleId(roleId);
    }

    @Override
    public List<SysMenuVo> findAllByTypeId(String typeId) {
        List<SysMenuVo> result = new ArrayList<>();
        List<SysMenu> list = sysMenuMapper.findMenuByType(typeId);
        if (list == null || list.size() == 0) {
            return result;
        }
        for (SysMenu sysMenu : list) {
            if (sysMenu.getMenuLevel() == 2) {
                for (SysMenu menu : list) {
                    if (sysMenu.getMenuParentId().equals(menu.getId())) {
                        result.add(new SysMenuVo(sysMenu.getId(), sysMenu.getMenuCode(), sysMenu.getMenuName(), sysMenu.getMenuDesc(), sysMenu.getMenuSort(), sysMenu.getMenuUrl(), sysMenu.getMenuIcon(), sysMenu.getRemarks(), sysMenu.getCreatedTime(), sysMenu.getCreatedUser(), sysMenu.getUpdateTime(), sysMenu.getUpdateUser(), sysMenu.getStatus(), sysMenu.getMenuLevel(), menu.getMenuName()));
                    }
                }
            }
        }
        return result;
    }

    @Override
    public SysMenu findById(String menuId) {
        return sysMenuMapper.selectByPrimaryKey(menuId);
    }

    /**
     *@Description 有选择性的修改单条记录
     *@Author  zhoujian
     *@Date 2018/9/18
     *@param   sysMenu 系统菜单
     *@return 
     */
    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
    }


   /* @Override
    public List<SysMenu> findAll() {
        SysMenuExample example = new SysMenuExample();
        example.createCriteria().andStatusEqualTo(1);
        return sysMenuMapper.selectByExample(example);
    }

    private void processMenu(List<SysMenu> list) {
        for (SysMenu sysMenu : list) {
            List<SysMenu> tempList = sysMenuMapper.findMenuAndFuncByType(2, sysMenu.getTypeId(), sysMenu.getId());
            if (tempList != null && !tempList.isEmpty()) {
                sysMenu.setMenuChild(tempList);
            }

        }
    }


    @Override
    public void addSysMenu(SysMenu sysMenu) {
        sysMenu.setId(UUIDUtils.uuid());
        sysMenu.setCreatedUser("admin");
        sysMenu.setCreatedTime(new Date());
        sysMenu.setUpdateUser("admin");
        sysMenu.setStatus(1);
        sysMenuMapper.insert(sysMenu);
    }

    @Transactional
    @Override
    public void updateSysMenu(SysMenu sysMenu) {
        sysMenu.setUpdateTime(new Date());
        sysMenu.setUpdateUser("system");
        sysMenuMapper.updateByPrimaryKey(sysMenu);
    }

    @Override
    public void deleteSysMenu(String id) {

        SysMenu newObbj = new SysMenu();
        newObbj.setId(id);
        newObbj.setStatus(0);
        sysMenuMapper.updateByPrimaryKeySelective(newObbj);
    }*/
}
